package com.webdev.base.util;

import cn.hutool.db.DaoTemplate;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.webdev.base.pojo.User;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TokenUtils {
    private static final String MY_SECRET = "mysecret";

    public static String getToken(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("alg", "HS256");
        map.put("typ", "JWT");
        return JWT.create().withHeader(map)// header
                .withClaim(Constants.TOKEN_USERID, id)// payload
                .sign(Algorithm.HMAC256(MY_SECRET));
    }

    public static void verifyToken(String token) throws JWTVerificationException {
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(MY_SECRET)).build();
        verifier.verify(token);
    }

    public static void main(String[] args) throws ParseException {
        User user = new User();
        user.setUserId(2101210228);
        String str = getToken(user.getUserId());
        System.out.println(str);
        try {
            verifyToken(str);
        } catch (JWTVerificationException e) {
            System.out.println("验证失败");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); // 设置北京时区
        Date d = sdf.parse(sdf.format(new Date()));
        System.out.println("d = " + d);
    }
}
